package com.online.edu.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.edu.course.entity.Category;
import com.online.edu.course.entity.CategoryMapping;
import com.online.edu.course.entity.Course;
import com.online.edu.course.entity.Vo.CategoryVo;
import com.online.edu.course.entity.Vo.CourseREQ;
import com.online.edu.course.entity.Vo.PageVo;
import com.online.edu.course.mapper.CourseMapper;
import com.online.edu.course.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.online.edu.course.utils.FileStoreUtil;
import com.online.edu.util.utils.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程表 服务实现类
 * </p>
 *
 * @author world
 * @since 2021-07-30
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    BaseMapper<Category> categoryBaseMapper;

    @Autowired
    BaseMapper<CategoryMapping> categoryMappingBaseMapper;

    @Autowired
    FileStoreUtil client;

    //    按页查询课程列表
    public List<CourseREQ> listPage(PageVo pageVo){
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        //传入当前页和页面记录数
        Page<Course> page = new Page<>(pageVo.getCurrent(), pageVo.getSize());
        //返回coursePage对象，里面封装了分页方法
        Page<Course> coursePage = this.page(page, queryWrapper);
        //设置总页数
        pageVo.setTotal(Math.toIntExact(coursePage.getTotal()));
        //获取当前页记录的对象
        List<Course> courseList = coursePage.getRecords();
        //将分页结果封装CourseREQ
        List<CourseREQ> categoryVos = courseList.stream().map(Course -> {
            CourseREQ courseREQ = new CourseREQ();
            BeanUtils.copyProperties(Course, courseREQ);
            return courseREQ;
        }).collect(Collectors.toList());

        //创建条件构造器
        QueryWrapper<CategoryMapping> wrapper = new QueryWrapper<>();

        List<CourseREQ> categoryResult = categoryVos.stream()
                .map(
                        courseREQ->{
                            //以课程ID为条件查询映射表
                            wrapper.clear();
                            wrapper.eq("course_id",courseREQ.getId());
                            //如果存在课程对应的分类
                            if(categoryMappingBaseMapper.selectList(wrapper)!=null) {


                                CategoryMapping categoryMapping = categoryMappingBaseMapper.selectOne(wrapper);
                                Category category1 = categoryBaseMapper.selectById(categoryMapping.getCategoryId());

//                                List<CategoryMapping> categoryMappings = categoryMappingBaseMapper.selectList(wrapper);
//                                //获得课程对应的所有课程分类
//                                List<Category> categories=categoryMappings.stream().map(
//                                        categoryMapping->categoryBaseMapper.selectById(categoryMapping.getCategoryId())
//                                ).collect(Collectors.toList());


//                                //树形查询
//                                List<CategoryVo> list = categories.stream()
//                                        .map(
//                                                category->{
//                                                    if (category.getParentId()!=0) {
//                                                        Category categoryP = categoryBaseMapper.selectById(category.getParentId());
//                                                        CategoryVo categoryVo = new CategoryVo();
//                                                        BeanUtils.copyProperties(category, categoryVo);
//                                                        categoryVo.setParent(getParentData(categoryP));
//                                                        return categoryVo;
//                                                    }else{
//                                                        CategoryVo categoryVo = new CategoryVo();
//                                                        BeanUtils.copyProperties(category, categoryVo);
//                                                        return categoryVo;
//                                                    }
//                                                })
//                                        .collect(Collectors.toList());

                                List<CategoryVo> list = new ArrayList<CategoryVo>();
                                list.add(getParentData(category1));


                                courseREQ.setCategoryVo(list);
                            }
                            //返回课程返回类
                            return courseREQ;
                        })
                .collect(Collectors.toList());
        return  categoryResult;
    }

    //获取孩子的方法，递归实现
    private CategoryVo getParentData(Category category) {

        if (category.getParentId()!=0) {
            Category categoryP = categoryBaseMapper.selectById(category.getParentId());
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            categoryVo.setParent(getParentData(categoryP));
            return categoryVo;
        }else{
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }
    }



    //根据课程id新增分类列表下的课程数量
    @Override
    public void addCategoryNums(long categoryId) {
        Category category = categoryBaseMapper.selectById(categoryId);
        category.setCategoryCourseNum(category.getCategoryCourseNum()+1);
        categoryBaseMapper.updateById(category);
    }

    @Override
    public String uploadFile(MultipartFile file) {
        //设置文件上传前面的拼接地址
        String url = "http://121.43.108.189:8080/";

        try {
            InputStream inputStream = file.getInputStream();
            String extName = StringUtil.getFileExtName(file.getOriginalFilename());
            String uploadPath = client.uploadFile(inputStream, extName);
            url = url + uploadPath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return url;
    }

    //新增课程时与分类列表绑定
    @Override
    public void addCategoryMapping(long categoryId, long courseId) {
        CategoryMapping categoryMapping = new CategoryMapping();
        categoryMapping.setCategoryMapping(Long.valueOf(StringUtil.uuid()));
        categoryMapping.setCategoryId(categoryId);
        categoryMapping.setCourseId(courseId);
        categoryMappingBaseMapper.insert(categoryMapping);
    }

    //    按页查询课程列表
    @Override
    public List<CourseREQ> listByWrapper(PageVo pageVo,QueryWrapper<Course> queryWrapper){
        //传入当前页和页面记录数
        Page<Course> page = new Page<>(pageVo.getCurrent(), pageVo.getSize());
        //返回coursePage对象，里面封装了分页方法
        Page<Course> coursePage = this.page(page, queryWrapper);
        //设置总页数
        pageVo.setTotal(Math.toIntExact(coursePage.getTotal()));
        //获取当前页记录的对象
        List<Course> courseList = coursePage.getRecords();
        //将分页结果封装CourseREQ
        List<CourseREQ> categoryVos = courseList.stream().map(Course -> {
            CourseREQ courseREQ = new CourseREQ();
            BeanUtils.copyProperties(Course, courseREQ);
            return courseREQ;
        }).collect(Collectors.toList());

        //创建条件构造器
        QueryWrapper<CategoryMapping> wrapper = new QueryWrapper<>();

        List<CourseREQ> categoryResult = categoryVos.stream()
                .map(
                        courseREQ->{
                            //以课程ID为条件查询映射表
                            wrapper.clear();
                            wrapper.eq("course_id",courseREQ.getId());
                            //如果存在课程对应的分类
                            if(categoryMappingBaseMapper.selectList(wrapper)!=null) {


                                CategoryMapping categoryMapping = categoryMappingBaseMapper.selectOne(wrapper);
                                Category category1 = categoryBaseMapper.selectById(categoryMapping.getCategoryId());

//                                List<CategoryMapping> categoryMappings = categoryMappingBaseMapper.selectList(wrapper);
//                                //获得课程对应的所有课程分类
//                                List<Category> categories=categoryMappings.stream().map(
//                                        categoryMapping->categoryBaseMapper.selectById(categoryMapping.getCategoryId())
//                                ).collect(Collectors.toList());
//
//                                //树形查询
//                                List<CategoryVo> list = categories.stream()
//                                        .map(
//                                                category->{
//                                                    if (category.getParentId()!=0) {
//                                                        Category categoryP = categoryBaseMapper.selectById(category.getParentId());
//                                                        CategoryVo categoryVo = new CategoryVo();
//                                                        BeanUtils.copyProperties(category, categoryVo);
//                                                        categoryVo.setParent(getParentData(categoryP));
//                                                        return categoryVo;
//                                                    }else{
//                                                        CategoryVo categoryVo = new CategoryVo();
//                                                        BeanUtils.copyProperties(category, categoryVo);
//                                                        return categoryVo;
//                                                    }
//                                                })
//                                        .collect(Collectors.toList());

                                List<CategoryVo> list = new ArrayList<CategoryVo>();
                                list.add(getParentData(category1));


                                courseREQ.setCategoryVo(list);
                            }
                            //返回课程返回类
                            return courseREQ;
                        })
                .collect(Collectors.toList());
        return  categoryResult;
    }
}
